/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceCoursewareCourseDao;
import com.lczyfz.istep.modules.resource.dao.ResourcePaperQuestionDao;
import com.lczyfz.istep.modules.resource.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 试卷-题目中间表Service
 * @author lsj
 * @version 2017-03-14
 */

/**
 * lsj
 */
@Service
@Transactional(readOnly = true)
public class ResourcePaperQuestionService extends CrudService<ResourcePaperQuestionDao, ResourcePaperQuestion> {

	@Autowired
	private ResourcePaperService resourcePaperService;

	@Autowired
	private ResourceQuestionService resourceQuestionService;
	@Autowired
	private ResourcePaperQuestionDao resourcePaperQuestionDao;

	public ResourcePaperQuestion get(String id) {
		return super.get(id);
	}

	public List<ResourcePaperQuestion> findList(ResourcePaperQuestion resourcePaperQuestion) {
		return super.findList(resourcePaperQuestion);
	}



	public Page<ResourcePaperQuestion> findPage(Page<ResourcePaperQuestion> page, ResourcePaperQuestion resourcePaperQuestion) {
		return super.findPage(page, resourcePaperQuestion);
	}
	
	@Transactional(readOnly = false)
	public void save(String duallist,ResourcePaper resourcePaper,String score) {
		if(Integer.valueOf(resourcePaper.getPaperType())== 1) {
			String[] resourceQuestionList = new String[0];
			if (duallist != null && !duallist.equals(""))
				resourceQuestionList = duallist.split(",");
			logger.debug("length: [{}]", resourceQuestionList.length);

//			resourcePaper.setQuestionSum(resourceQuestionList.length + "");

			int count = 0;
			for (int i = 0; i < resourceQuestionList.length; i++) {
				ResourceQuestion resourceQuestion = resourceQuestionService.get(resourceQuestionList[i]);
				if (resourceQuestion.getEnvironmentId() != null && !resourceQuestion.getEnvironmentId().equals("")) {
					count++;
				}
			}
//			resourcePaper.setExperimentSum(count + "");

			resourcePaperService.save(resourcePaper);
			for (int i = 0; i < resourceQuestionList.length; i++) {

				ResourcePaperQuestion resourcePaperQuestion = new ResourcePaperQuestion();
				resourcePaperQuestion.setPaperId(resourcePaper.getId());
				resourcePaperQuestion.setScore(Integer.valueOf(score));
				logger.debug("fdsafLSJJFKLDSJKL ");
				resourcePaperQuestion.setQuestionId(resourceQuestionList[i]);
				super.save(resourcePaperQuestion);
			}

		}

	}

	@Transactional(readOnly = false)
	public void saveRandom(ArrayList<ResourceQuestion> list,ResourcePaper resourcePaper) {
//		String[] resourceQuestionList = new String[0];
//		if (duallist != null && !duallist.equals(""))
//			resourceQuestionList = duallist.split(",");
//		logger.debug("length: [{}]", resourceQuestionList.length);

		resourcePaper.setQuestionSum(list.size()+"");

		int count=0;

		for(ResourceQuestion rQ:list)
		{
			ResourceQuestion resourceQuestion = resourceQuestionService.get(rQ.getId());
			if(resourceQuestion.getEnvironmentId()!=null && !resourceQuestion.getEnvironmentId().equals(""))
			{
				count++;
			}
		}
		resourcePaper.setExperimentSum(count + "");

		resourcePaperService.save(resourcePaper);
		for (ResourceQuestion rQ:list) {

			ResourcePaperQuestion resourcePaperQuestion=new ResourcePaperQuestion();
			resourcePaperQuestion.setPaperId(resourcePaper.getId());
			resourcePaperQuestion.setQuestionId(rQ.getId());
			System.out.println("==============================");

			if (resourcePaperQuestion != null && !resourcePaperQuestion.equals("")) {
				resourcePaperQuestion.setScore(Integer.parseInt(rQ.getQuestionScore()));
				System.out.println("==============================" +resourcePaperQuestion.getScore());
			}
			super.save(resourcePaperQuestion);
		}



	}

	@Transactional(readOnly = false)
	public void update(String duallist,ResourcePaper resourcePaper,String score) {
		ResourcePaperQuestion temp=new ResourcePaperQuestion();
		temp.setPaperId(resourcePaper.getId());
		//根据resourcePaper的id返回之前插入的多条记录
		List<ResourcePaperQuestion> resourcePaperQuestionsList=super.findList(temp);
		logger.debug("进来了！！！");
		logger.debug("试卷-题目中间表数量: [{}]", resourcePaperQuestionsList.size());
		//统计题目总数
		String[] resourceQuestionList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceQuestionList = duallist.split(",");
		logger.debug("length: [{}]", resourceQuestionList.length);
		logger.debug("进来了？？？？");
		resourcePaper.setQuestionSum(resourceQuestionList.length+"");
	//统计实验题目数量
		int count=0;
		for(int i=0; i<resourceQuestionList.length;i++)
		{
			ResourceQuestion resourceQuestion = resourceQuestionService.get(resourceQuestionList[i]);
			if(resourceQuestion.getEnvironmentId()!=null && !resourceQuestion.getEnvironmentId().equals(""))
			{
				count++;
			}
		}
		resourcePaper.setExperimentSum(count + "");
		resourcePaperService.save(resourcePaper);
//		for (int i = 0; i < resourcePaperQuestionsList.size(); i++) {
//			super.delete(resourcePaperQuestionsList.get(i));
//		}

		for (int i = 0; i < resourceQuestionList.length; i++) {
			ResourcePaperQuestion resourcePaperQuestion = new ResourcePaperQuestion();
			resourcePaperQuestion.setPaperId(resourcePaper.getId());
			resourcePaperQuestion.setScore(Integer.valueOf(score));
			resourcePaperQuestion.setQuestionId(resourceQuestionList[i]);
			super.save(resourcePaperQuestion);
		}
//		ResourcePaperQuestion resourcePaperQuestion = new ResourcePaperQuestion();
//		resourcePaperQuestion.setPaperId(resourcePaper.getId());
//		resourcePaperQuestion.setScore(Integer.valueOf(score));
//		resourcePaperQuestion.setQuestionId(resourceQuestionList[0]);
//		super.save(resourcePaperQuestion);

	}

	//根据paperId删除试卷题目表数据-真删除
	@Transactional(readOnly = false)
	public void deleteByPaperId(ResourcePaper resourcePaper) {
		ResourcePaperQuestion temp=new ResourcePaperQuestion();
		temp.setPaperId(resourcePaper.getId());
		//根据resourcePaper的id返回之前插入的多条记录
		List<ResourcePaperQuestion> resourcePaperQuestionsList=super.findList(temp);
		for (int i = 0; i < resourcePaperQuestionsList.size(); i++) {
			resourcePaperQuestionDao.deleteByPaperId(resourcePaper.getId());
		}
	}


	@Transactional(readOnly = false)
	public void delete(ResourcePaperQuestion resourcePaperQuestion) {
		super.delete(resourcePaperQuestion);
	}
	
}